We need to create our entity model. There are three classes in it:
Address
Company
Person
To create the basic classes:
Select the src folder under <project>
Right-click and select New::Class
For the package, enter entity
For the name, enter Address, Company, and Person respectively.
Type or enter the source provided below for each of the three classes.
Changes
The entity model is changed slightly from the first JPA tutorial. There is one change in Company.java. Review that class' comments to understand that change. The quick summary is that we've added eager fetching to a relationship.
Address.java
packageentity;importjavax.persistence.Embeddable;/**
* There are no changes to this entity. Its embedded in the Person and Company
* and as such, it is automatically fetched.
*/
@Embeddable
publicclass Address {privateString streetAddress1;privateString streetAddress2;privateString city;privateString state;privateString zip;public Address(){}public Address(finalString sa1, finalString sa2, finalString city,
finalString state, finalString zip){
setStreetAddress1(sa1);
setStreetAddress2(sa2);
setCity(city);
setState(state);
setZip(zip);}publicString getCity(){return city;}publicvoid setCity(finalString city){this.city = city;}publicString getState(){return state;}publicvoid setState(finalString state){this.state = state;}publicString getStreetAddress1(){return streetAddress1;}publicvoid setStreetAddress1(finalString streetAddress1){this.streetAddress1 = streetAddress1;}publicString getStreetAddress2(){return streetAddress2;}publicvoid setStreetAddress2(finalString streetAddress2){this.streetAddress2 = streetAddress2;}publicString getZip(){return zip;}publicvoid setZip(finalString zip){this.zip = zip;}}
Company.java
packageentity;importjava.util.ArrayList;importjava.util.Collection;importjava.util.List;importjavax.persistence.CascadeType;importjavax.persistence.Embedded;importjavax.persistence.Entity;importjavax.persistence.FetchType;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.OneToMany;/**
* There is one significant change to this Entity from the one we used in the
* first JPA tutorial. Review the employees attribute for details on what and
* why.
*/
@Entitypublicclass Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)privateLong id;privateString name;
@Embedded
private Address address;/**
* This attribute has one important (and significant change) from the
* original Company.java. We've added eager fetching to this relationship.
* By default, one to many relationships are lazily loaded. This means the
* collection will not actually be brought back from the database unless it
* is specifically used. So long as the Company instance is managed, this is
* not a problem. Once the object is no longer managed, however, lazily
* loaded references that have not been touched are not available (according
* to the specification, what happens is undefined).
*
* In the previous example, we did everything in a single transaction. But
* now we have a transaction starting and stopping on each method in the
* session bean, so as soon as we return from one of the session bean
* methods, the transaction is closed. Once the transaction is closed, all
* managed objects are flushed from the entity manager (and therefore no
* longer managed).
*/
@OneToMany(mappedBy = "job", cascade = { CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH}, fetch = FetchType.EAGER)privateCollection<Person> employees = newArrayList<Person>();public Company(){}public Company(finalString name, final Address address,
final Person... persons){
setName(name);
setAddress(address);for(Person p : persons){
getEmployees().add(p);}}public Address getAddress(){return address;}publicvoid setAddress(final Address address){this.address = address;}publicCollection<Person> getEmployees(){return employees;}publicvoid setEmployees(finalList<Person> newStaff){// fire everybodyfinalList<Person> clone = newArrayList<Person>(employees);for(Person p : clone){
fire(p);}for(Person p : newStaff){
hire(p);}}publicLong getId(){return id;}publicvoid setId(finalLong id){this.id = id;}publicString getName(){return name;}publicvoid setName(finalString name){this.name = name;}publicvoid hire(final Person p){
employees.add(p);
p.setJob(this);}publicvoid fire(final Person p){
employees.remove(p);
p.setJob(null);}
@Overridepublicboolean equals(finalObject rhs){if(rhs instanceof Company){return((Company) rhs).getId() == getId();}returnfalse;}
@Overridepublicint hashCode(){return101* getId().hashCode();}}
Person.java
packageentity;importjavax.persistence.Embedded;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.ManyToOne;/**
* Give the tests and assertions so far, there's no need to fix this problem.
*/
@Entitypublicclass Person {
@Id
@GeneratedValue
int id;privateString firstName;privatechar middleInitial;privateString lastName;
@Embedded
private Address address;
@ManyToOne
private Company job;public Person(){}public Person(finalString fn, finalchar mi, finalString ln,
final Address address){
setFirstName(fn);
setMiddleInitial(mi);
setLastName(ln);
setAddress(address);}publicString getFirstName(){return firstName;}publicvoid setFirstName(finalString firstName){this.firstName = firstName;}publicint getId(){return id;}publicvoid setId(finalint id){this.id = id;}publicString getLastName(){return lastName;}publicvoid setLastName(finalString lastName){this.lastName = lastName;}publicchar getMiddleInitial(){return middleInitial;}publicvoid setMiddleInitial(finalchar middleInitial){this.middleInitial = middleInitial;}public Address getAddress(){return address;}publicvoid setAddress(final Address address){this.address = address;}public Company getJob(){return job;}publicvoid setJob(Company job){this.job = job;}
@Overridepublicboolean equals(finalObject rhs){if(rhs instanceof Person){return((Person) rhs).getId() == getId();}returnfalse;}
@Overridepublicint hashCode(){return101* getId();}}
Creating the Entity Model
We need to create our entity model. There are three classes in it:To create the basic classes:
Type or enter the source provided below for each of the three classes.
Changes
The entity model is changed slightly from the first JPA tutorial. There is one change in Company.java. Review that class' comments to understand that change. The quick summary is that we've added eager fetching to a relationship.Address.java
Company.java
Person.java